package com.weipu.common.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public final class ThreadUtil
{
	
    private ExecutorService executorService;
    
    private static ThreadUtil inst = new ThreadUtil();
    
//    private  ExecutorService threadPoll=Executors.newFixedThreadPool(5);
    
    /** 
     * @Fields POOL_SIZE : 单个CPU线程池大小
     */
    private static final int POOL_SIZE = 4;
    
    private int supportMaxPoolSize;//最大支持线程大小，Cache模式下为无限制 -1
    
    public int getSupportMaxPoolSize()
    {
        return supportMaxPoolSize;
    }
    
    public static ThreadUtil getInstance()
    {
        return inst;
    }
    
    private ThreadUtil()
    {
        //获取当前系统的CPU 数目 
        int cpuNums = Runtime.getRuntime().availableProcessors();
        
        if (Logger.isLog)
        {
            Logger.i("ThreadUtil() - cpuNums: " + cpuNums);
        }
        
        if (cpuNums >= 3)
        {
            //executorService = Executors.newCachedThreadPool();
            //supportMaxPoolSize = -1;
            supportMaxPoolSize=10;
            executorService = Executors.newFixedThreadPool(supportMaxPoolSize);
            if (Logger.isLog)
            {
                Logger.i("ThreadUtil() - pool.size is default.");
            }
        }
        else
        {
            supportMaxPoolSize = cpuNums * POOL_SIZE;
            
            //ExecutorService通常根据系统资源情况灵活定义线程池大小 
            executorService = Executors.newFixedThreadPool(supportMaxPoolSize);
            
        }
        
        if (Logger.isLog)
        {
            Logger.i("ThreadUtil() - pool.size: " + supportMaxPoolSize);
        }
        
    }
    
    public ExecutorService getThreadPoolIntance()
    {
        return executorService;
    }
    
//    public ExecutorService getExecutorServiceIntance()
//    {
//        return threadPoll;
//    }
}
